{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 246,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: [5, 4, 3], 2: [8, 9, 10]}"
      ]
     },
     "execution_count": 246,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "money = [5,4,8,9,10,3]\n",
    "L = 5\n",
    "labelnum = round(max(money) / L)\n",
    "label = {}\n",
    "for i in money:\n",
    "    for j in range(1, labelnum+1):\n",
    "        if round(i / L) == j:\n",
    "            if j not in label.keys():\n",
    "                label[j] = [i]\n",
    "            else:\n",
    "                label[j].append(i)\n",
    "label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.36787944,  0.36787944],\n",
       "       [ 0.36787944,  0.36787944],\n",
       "       [ 0.18393972,  0.18393972],\n",
       "       [ 0.06131324,  0.06131324]])"
      ]
     },
     "execution_count": 247,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as st\n",
    "arr = np.zeros((len(label[1])+1,labelnum))\n",
    "# 假设λ=1\n",
    "rv = st.poisson(1)\n",
    "for i in range(arr.shape[0]):\n",
    "    for j in range(arr.shape[1]):\n",
    "        arr[i][j] = rv.pmf(i)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.13533528,  0.13533528,  0.06766764,  0.02255588],\n",
       "       [ 0.13533528,  0.13533528,  0.06766764,  0.02255588],\n",
       "       [ 0.06766764,  0.06766764,  0.03383382,  0.01127794],\n",
       "       [ 0.02255588,  0.02255588,  0.01127794,  0.00375931]])"
      ]
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pro = np.zeros((arr.shape[0], arr.shape[0]))\n",
    "loss = np.zeros((arr.shape[0], arr.shape[0]))\n",
    "for i in range(pro.shape[0]):\n",
    "    for j in range(pro.shape[1]):\n",
    "        loss[i, j] = i * L + j * 2* L\n",
    "        pro[i, j] = arr[i, 0] * arr[j, 1]\n",
    "pro"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0.,  10.,  20.,  30.],\n",
       "       [  5.,  15.,  25.,  35.],\n",
       "       [ 10.,  20.,  30.,  40.],\n",
       "       [ 15.,  25.,  35.,  45.]])"
      ]
     },
     "execution_count": 276,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0.0: 0.1353352832366127,\n",
       " 5.0: 0.1353352832366127,\n",
       " 10.0: 0.20300292485491905,\n",
       " 15.0: 0.15789116377604814,\n",
       " 20.0: 0.13533528323661267,\n",
       " 25.0: 0.090223522157741792,\n",
       " 30.0: 0.056389701348588617,\n",
       " 35.0: 0.033833820809153176,\n",
       " 40.0: 0.011277940269717724,\n",
       " 45.0: 0.0037593134232392421}"
      ]
     },
     "execution_count": 277,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = {}\n",
    "for i in range(pro.shape[0]):\n",
    "    for j in range(pro.shape[1]):\n",
    "        if loss[i, j] not in arr2.keys():\n",
    "            arr2[loss[i, j]] = pro[i, j]\n",
    "        else:\n",
    "            arr2[loss[i, j]] = (arr2[loss[i, j]] + pro[i, j])\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.3533528323661268 0.9371554423371553\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>违约总损失</th>\n",
       "      <th>联合概率密度</th>\n",
       "      <th>累计概率密度</th>\n",
       "      <th>期望损失</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.135335</td>\n",
       "      <td>0.135335</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.0</td>\n",
       "      <td>0.135335</td>\n",
       "      <td>0.270671</td>\n",
       "      <td>0.676676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.0</td>\n",
       "      <td>0.203003</td>\n",
       "      <td>0.473673</td>\n",
       "      <td>2.030029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15.0</td>\n",
       "      <td>0.157891</td>\n",
       "      <td>0.631565</td>\n",
       "      <td>2.368367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20.0</td>\n",
       "      <td>0.135335</td>\n",
       "      <td>0.766900</td>\n",
       "      <td>2.706706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>25.0</td>\n",
       "      <td>0.090224</td>\n",
       "      <td>0.857123</td>\n",
       "      <td>2.255588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>30.0</td>\n",
       "      <td>0.056390</td>\n",
       "      <td>0.913513</td>\n",
       "      <td>1.691691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>35.0</td>\n",
       "      <td>0.033834</td>\n",
       "      <td>0.947347</td>\n",
       "      <td>1.184184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>40.0</td>\n",
       "      <td>0.011278</td>\n",
       "      <td>0.958625</td>\n",
       "      <td>0.451118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>45.0</td>\n",
       "      <td>0.003759</td>\n",
       "      <td>0.962384</td>\n",
       "      <td>0.169169</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   违约总损失    联合概率密度    累计概率密度      期望损失\n",
       "0    0.0  0.135335  0.135335  0.000000\n",
       "1    5.0  0.135335  0.270671  0.676676\n",
       "2   10.0  0.203003  0.473673  2.030029\n",
       "3   15.0  0.157891  0.631565  2.368367\n",
       "4   20.0  0.135335  0.766900  2.706706\n",
       "5   25.0  0.090224  0.857123  2.255588\n",
       "6   30.0  0.056390  0.913513  1.691691\n",
       "7   35.0  0.033834  0.947347  1.184184\n",
       "8   40.0  0.011278  0.958625  0.451118\n",
       "9   45.0  0.003759  0.962384  0.169169"
      ]
     },
     "execution_count": 280,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame()\n",
    "df['违约总损失'] = arr2.keys()\n",
    "df['联合概率密度'] = arr2.values()\n",
    "df = df.sort_values(by='违约总损失')\n",
    "df.reset_index(drop=True,inplace=True)\n",
    "df.loc[0, '累计概率密度'] = df.loc[0, '联合概率密度']\n",
    "for i in range(1, df.shape[0]):\n",
    "    df.loc[i, '累计概率密度'] = df.loc[i-1, '累计概率密度'] + df.loc[i, '联合概率密度'] \n",
    "df['期望损失'] = df['违约总损失'] * df['联合概率密度']\n",
    "avg = np.mean(df['期望损失'])\n",
    "std = np.std(df['期望损失'])\n",
    "print(avg,std)\n",
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
